package com.quanshu.exchange.ui.gui.profile;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.LoginInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改登录密码
 * Created by xiniu003 on 2017/12/13.
 */

public class AmendEnterPasswordActivity extends BaseActivity {

    @BindView(R.id.original_enter_password)
    EditText originalEnterPassword;
    @BindView(R.id.new_enter_password)
    EditText newEnterPassword;
    @BindView(R.id.enter_amend)
    Button enterAmend;
    @BindView(R.id.set_login_pwd)
    BaseTitleBar setLoginPwd;
    @BindView(R.id.enter_amend1)
    TextView enterAmend1;

    private String originalEnterPasswordStr;
    private String newEnterPasswordStr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amend_enter_password);
        ButterKnife.bind(this);

        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initListener() {
        ImmersionBar.with(this).titleBar(setLoginPwd).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        setLoginPwd.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("重置登录密码").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        RxViewHelper.getInstance().afterTextChangeEvents(originalEnterPassword,
                textViewAfterTextChangeEvent -> {
                    originalEnterPasswordStr = textViewAfterTextChangeEvent.editable().toString();
                });

        RxViewHelper.getInstance().afterTextChangeEvents(newEnterPassword,
                textViewAfterTextChangeEvent -> {
                    newEnterPasswordStr = textViewAfterTextChangeEvent.editable().toString();
                });

        RxViewHelper.getInstance().clicks(enterAmend1, aVoid -> editLoginPwd());
    }

    //修改登录密码
    private void editLoginPwd() {
        if (!TextUtils.isEmpty(originalEnterPasswordStr) && !TextUtils.isEmpty(newEnterPasswordStr)) {
            NetClient.getInstance().getShopService(LoginInterface.class)
                    .editLoginPwd(AppUtils.getSignParams(currentUser.getMemberid(),
                            originalEnterPasswordStr,
                            newEnterPasswordStr),
                            currentUser.getMemberid(),
                            originalEnterPasswordStr,
                            newEnterPasswordStr)
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse>() {
                        @Override
                        public void onSuccess(BaseResponse response) {

                            ToastHelper.toast("修改成功!");
                            finish();
                        }
                    });
        }
    }
}
